package com.ge.modules.api.controller;

import com.ge.common.annotation.AuthIgnoreApi;
import com.ge.common.utils.DeleteType;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.service.TbCommodityService;
import com.ge.modules.entity.TbCommodityEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * 商品
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-04-11 17:29:31
 */
@RestController
@RequestMapping("/api/user/commodity")
public class TbCommodityController {
    @Autowired
    private TbCommodityService tbCommodityService;

    /**
     * 列表
     * 允许根据 商品名称和类型 进行查询
     * 如params.put("name","商品名称");
     */
    @AuthIgnoreApi
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询已上架的商品
		params.put("putAwayStatus",1);
        params.put("deleteType",DeleteType.NOT_DELETE.getKey());
        //查询列表数据
        Query query = new Query(params);
        try {
            List<TbCommodityEntity> tbCommodityEntityList = tbCommodityService.queryList(query);
            int total = tbCommodityService.queryTotal(query);
            PageUtils pageUtil = new PageUtils(tbCommodityEntityList, total, query.getLimit(), query.getPage());
            return R.ok().put("data", pageUtil);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 查询商品列表 不分页
     *
     * @param tbCommodityEntity
     * @return
     */
    @AuthIgnoreApi
    @RequestMapping("/listInfo")
    public R listInfo(TbCommodityEntity tbCommodityEntity) {
        tbCommodityEntity.setPutAwayStatus(1);
        //查询列表数据
        List<TbCommodityEntity> tbCommodityEntityList = tbCommodityService.queryListObject(tbCommodityEntity);
        return R.ok().put("tbCommodityEntityList", tbCommodityEntityList);
    }

    /**
     * 查询商品详细信息
     *
     * @param commodityId
     * @return
     */
    @AuthIgnoreApi
    @RequestMapping("/info/{commodityId}")
    public R info(@PathVariable("commodityId") Integer commodityId) {
        //通过对象存储条件
        TbCommodityEntity tbCommodityEntity1 = new TbCommodityEntity();
        tbCommodityEntity1.setCommodityId(commodityId);
        try {
            TbCommodityEntity tbCommodityEntity = tbCommodityService.queryObject(tbCommodityEntity1);
            return R.ok().put("data", tbCommodityEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

}
